package com.quad.innovators.salesease.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quad.innovators.salesease.common.utils.SystemCodeUtils;
import com.quad.innovators.salesease.converter.SaleOrderDetailConverter;
import com.quad.innovators.salesease.enums.SystemCodeEnum;
import com.quad.innovators.salesease.mapper.warehouse.SaleOrderDetailMapper;
import com.quad.innovators.salesease.model.entity.warehouse.SaleOrderDetail;
import com.quad.innovators.salesease.model.form.warehouse.SaleOrderDetailForm;
import com.quad.innovators.salesease.model.vo.warehouse.SaleReturnScanNumberVO;
import com.quad.innovators.salesease.model.vo.warehouse.ScanCodeVO;
import com.quad.innovators.salesease.service.warehouse.SaleOrderDetailService;
import com.quad.innovators.salesease.service.warehouse.SaleOrderScanService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/26 10:28
 * @Version : 1.0 
 */
@Service
@RequiredArgsConstructor
public class SaleOrderDetailServiceImpl extends ServiceImpl<SaleOrderDetailMapper, SaleOrderDetail> implements SaleOrderDetailService {

    private final SaleOrderDetailConverter saleOrderDetailConverter;

    private final SaleOrderScanService saleOrderScanService;

    @Override
    public List<SaleOrderDetailForm> getListBySaleReturnCode(String code) {
        List<SaleOrderDetail> saleOrderDetailList = this.baseMapper.selectList(new QueryWrapper<SaleOrderDetail>().lambda().eq(SaleOrderDetail::getSaleReturnCode, code));
        List<String> stringList = saleOrderDetailList.stream().map(SaleOrderDetail::getCode).toList();
        if (CollectionUtils.isNotEmpty(stringList)) {
            List<SaleReturnScanNumberVO> scanNumberByDetailCodeList = saleOrderScanService.getScanNumberInDetailCodeList(stringList);
            Map<String, Integer> stringIntegerMap = scanNumberByDetailCodeList.stream().collect(Collectors.toMap(SaleReturnScanNumberVO::getSaleReturnOrderDetailCode, SaleReturnScanNumberVO::getSaleReturnOrderNumber));
            return saleOrderDetailList.stream().map(model -> {
                SaleOrderDetailForm saleOrderDetailForm = saleOrderDetailConverter.toForm(model);
                saleOrderDetailForm.setScanProductNumberFlag(stringIntegerMap.getOrDefault(model.getCode(), 0).equals(model.getProductNumber()));
                return saleOrderDetailForm;
            }).collect(Collectors.toList());
        }
        return saleOrderDetailList.stream().map(saleOrderDetailConverter::toForm).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveList(List<SaleOrderDetailForm> saleOrderDetailFormList, String code) {
        // 销售和退货成功一个即可
        if (CollectionUtils.isEmpty(saleOrderDetailFormList)) {
            return true;
        }
        return this.saveBatch(saleOrderDetailFormList.stream().map(model -> {
            SaleOrderDetail entity = saleOrderDetailConverter.toEntity(model);
            entity.setCode(SystemCodeUtils.getCode(SystemCodeEnum.SALE_ORDER_DETAIL));
            entity.setSaleReturnCode(code);
            return entity;
        }).toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateList(String code, List<SaleOrderDetailForm> saleOrderDetailFormList) {
        boolean remove = this.remove(new QueryWrapper<SaleOrderDetail>().lambda().eq(SaleOrderDetail::getSaleReturnCode, code));
        return remove && saveList(saleOrderDetailFormList, code);
    }

    @Override
    public List<ScanCodeVO> getScanDetail(String saleOrderDetailCode) {
        return saleOrderScanService.getScanDetail(saleOrderDetailCode);
    }
}
